package com.jzo2o.customer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.BeanUtils;
import com.jzo2o.common.utils.StringUtils;
import com.jzo2o.customer.mapper.AgencyCertificationAuditMapper;
import com.jzo2o.customer.mapper.AgencyCertificationMapper;
import com.jzo2o.customer.mapper.WorkerCertificationAuditMapper;
import com.jzo2o.customer.model.domain.AgencyCertification;
import com.jzo2o.customer.model.domain.AgencyCertificationAudit;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.service.IAgencyCertificationAuditService;
import com.jzo2o.customer.service.IWorkerCertificationAuditService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @Author: Dearsil
 * @Date: 2025/1/13 16:07
 * @Version: 1.0
 * @Description: Hello Description!
 */
@Service
public class AgencyCertificationAuditServiceImpl extends ServiceImpl<AgencyCertificationAuditMapper, AgencyCertificationAudit> implements IAgencyCertificationAuditService {
    @Autowired
    private AgencyCertificationMapper agencyCertificationMapper;

    @Autowired
    private AgencyCertificationAuditServiceImpl owner;

    @Override
    public PageResult pageQuery(AgencyCertificationAuditPageQueryReqDTO dto) {
        //设置分页条件
        Page<AgencyCertificationAudit> page = PageUtils.parsePageQuery(dto, AgencyCertificationAudit.class);

        //设置业务条件
        LambdaQueryWrapper<AgencyCertificationAudit> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dto.getAuditStatus() != null, AgencyCertificationAudit::getAuditStatus, dto.getAuditStatus());
        queryWrapper.eq(dto.getCertificationStatus() != null, AgencyCertificationAudit::getCertificationStatus, dto.getCertificationStatus());
        queryWrapper.like(StringUtils.isNotEmpty(dto.getLegalPersonName()),AgencyCertificationAudit::getLegalPersonName, dto.getLegalPersonName());
        queryWrapper.like(StringUtils.isNotEmpty(dto.getName()), AgencyCertificationAudit::getName, dto.getName());

        //执行分页查询
        page = this.page(page, queryWrapper);

        //返回结果
        return PageUtils.toPage(page, AgencyCertificationAudit.class);
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void auditPassOrReject(Long id, Integer certificationStatus, String rejectReason) {
        //修改审核表中的信息
        boolean update = owner.lambdaUpdate()
                .eq(AgencyCertificationAudit::getId, id)
                .set(AgencyCertificationAudit::getAuditStatus, 1)
                .set(AgencyCertificationAudit::getAuditorId, UserContext.currentUserId())
                .set(AgencyCertificationAudit::getAuditorName, UserContext.currentUser().getName())
                .set(AgencyCertificationAudit::getAuditTime, LocalDateTime.now())
                .set(AgencyCertificationAudit::getCertificationStatus, certificationStatus)
                .set(StringUtils.isNotEmpty(rejectReason), AgencyCertificationAudit::getRejectReason, rejectReason)
                .update();
        if (!update) {
            throw new RuntimeException("审核失败");
        }

        //修改认证表中的信息
        AgencyCertificationAudit agencyCertificationAudit = this.getById(id);
        AgencyCertification agencyCertification = BeanUtils.copyProperties(agencyCertificationAudit, AgencyCertification.class);
        agencyCertification.setId(agencyCertificationAudit.getServeProviderId());
        agencyCertification.setCertificationTime(LocalDateTime.now());

        agencyCertificationMapper.updateById(agencyCertification);
    }
}
